//
//  HECaseListViewController.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/6/18.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HECaseListViewController.h"
#import "HECaseReportModel.h"
#import "HECaseReportCell.h"
#import "HEUploadCaseViewController.h"
#import "HECaseReportDetailsViewController.h"

@interface HECaseListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HECaseReportModel *> * dataSourceArr;

@end

@implementation HECaseListViewController

- (NSMutableArray<HECaseReportModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"病历资料";
    
    [self initUI];
    [self loadData];
    
    [self setCustomTableView:_tableView];
}

- (void)initUI {
  
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight - ZXSafeAreaBottom) style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");
    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    [_tableView setContentInset:UIEdgeInsetsMake(0,0, 30 + ZXSafeAreaBottom, 0)];

    self.tableView.backgroundColor = CViewBgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        weakSelf.isFromHeadRefresh = YES;
        weakSelf.pageNum = 1;
        [weakSelf loadData];
    }];
    [_tableView registerClass:[HECaseReportCell class] forCellReuseIdentifier: KHECaseReportCell];
    
    
    // 上传病历
    QMUIFillButton *submitBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    submitBtn.titleLabel.font = UIFontMake(16);
    [submitBtn setTitle:@"去上传病历" forState:UIControlStateNormal];
    [self.view addSubview:submitBtn];
    [submitBtn addTarget:self action:@selector(submitApply) forControlEvents:UIControlEventTouchUpInside];
    
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-(30 + ZXSafeAreaBottom));
        make.left.equalTo(self.view).offset(40);
        make.right.equalTo(self.view).offset(-40);
        make.height.mas_offset(45);
    }];
}

#pragma mark-

- (void)loadData {
    
    NSMutableDictionary *params = @{}.mutableCopy;
    [params setValue:@"qryPhrUserCase" forKey:@"service"];
    
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue: _usid forKey:@"usid"];
    [params setValue:dict forKey:@"params"];
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KReportData parameters:params success:^(id  _Nonnull responseObject) {

        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HECommonResponse *response = [HECommonResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HECaseReportModel mj_objectArrayWithKeyValuesArray:response.data.result]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.result.count == 0) {
                [Dialog toast:@"没有更多数据了~" delay:2.0];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)submitApply {
    
    HEUploadCaseViewController *vc = [HEUploadCaseViewController new];
    vc.usid = _usid;
    vc.backBlock = ^{
        [self.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HECaseReportCell *cell = [tableView dequeueReusableCellWithIdentifier: KHECaseReportCell];
    [cell configCellWithModel: self.dataSourceArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHECaseReportCell cacheByIndexPath:indexPath configuration:^(HECaseReportCell *cell) {
        [cell configCellWithModel: self.dataSourceArr[indexPath.row]];
    }];
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HECaseReportModel *model = _dataSourceArr[indexPath.row];
    HECaseReportDetailsViewController *vc = [HECaseReportDetailsViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}


@end

